package org.tokaf.rater;

import org.tokaf.TopKElement;
import org.tokaf.datasearcher.DataSearcher;

/**
 * <p>MinimumRater computes the rating as minimum of all rating of attributes of object.</p>
 * <p>Copyright (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class MinimumRater extends Rater {

	public MinimumRater(String fields[]) {
		this.fields = (String[]) fields.clone();
	}

	public double getDerivation(int index, DataSearcher[] data, TopKElement el) {
		for (int i = 0; i < el.getLength(); i++) {
			if (el.getRating(i) < el.getRating(index)) {
				return 0;
			}
		}
		return 1;
	}

	public double getRating(TopKElement el, DataSearcher[] data, double optional) {
		double a = Double.MAX_VALUE;
		for (int i = 0; i < el.getLength(); i++) {
			if (el.isNull(i)) {
				if (a == Double.MAX_VALUE)
					a = optional;
				else if (optional < a)
					a = optional;
			} else {
				if (a == Double.MAX_VALUE)
					a = el.getRating(i);
				else if (el.getRating(i) < a)
					a = el.getRating(i);
			}
		}
		return a;
	}

	public double getRating(TopKElement el, DataSearcher[] data,
			TopKElement optional) {
		double a = Double.MAX_VALUE;
		for (int i = 0; i < el.getLength(); i++) {
			if (el.isNull(i)) {
				if (a == Double.MAX_VALUE)
					a = optional.getRating(i);
				else if (optional.getRating(i) < a)
					a = optional.getRating(i);
			} else {
				if (a == Double.MAX_VALUE)
					a = el.getRating(i);
				else if (el.getRating(i) < a)
					a = el.getRating(i);
			}
		}
		return a;
	}
}
